package model;

public class Pacman {
    private int row, col;
    private int dirRow = 0, dirCol = 0;
    private int nextDirRow = 0, nextDirCol = 0;

    public Pacman(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public void setDirection(int keyCode) {
        switch (keyCode) {
            case 37: nextDirRow = 0; nextDirCol = -1; break; // 左
            case 38: nextDirRow = -1; nextDirCol = 0; break; // 上
            case 39: nextDirRow = 0; nextDirCol = 1; break; // 右
            case 40: nextDirRow = 1; nextDirCol = 0; break; // 下
        }
    }

    public boolean move(model.Map map) {
        int newRow = row + nextDirRow;
        int newCol = col + nextDirCol;
        if (!map.isWall(newRow, newCol)) {
            dirRow = nextDirRow;
            dirCol = nextDirCol;
        }
        newRow = row + dirRow;
        newCol = col + dirCol;
        if (!map.isWall(newRow, newCol)) {
            row = newRow;
            col = newCol;
            return map.eatDot(row, col);
        }
        return false;
    }

    public int getRow() { return row; }
    public int getCol() { return col; }
    public void setPosition(int row, int col) { this.row = row; this.col = col; }
    public int[] getFront(int n, model.Map map) {
        int r = row, c = col;
        for (int i = 0; i < n; i++) {
            int nr = r + dirRow;
            int nc = c + dirCol;
            if (map.isWall(nr, nc)) break;
            r = nr;
            c = nc;
        }
        return new int[]{r, c};
    }
} 